package site.beanyon.now.aspect.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import site.beanyon.now.model.dto.TimeCardDTO;
import site.beanyon.now.model.po.TimeCardPO;
import site.beanyon.now.service.UserService;

import java.util.List;

/**
 * 时间卡片日志切面
 */
@Aspect
@Component
class TimeCardLogAspect {
    private Logger logger = LoggerFactory.getLogger(TimeCardLogAspect.class);

    @Autowired
    private UserService userService;

    /**
     * 获取时间卡片列表
     *
     * @param jp 连接点
     * @return 时间卡片列表
     */
    @Around("execution(* site.beanyon.now.service.TimeCardService.listTimeCard(..))")
    private List<TimeCardDTO> listTimeCard(ProceedingJoinPoint jp) throws Throwable {
        // 获取前
        Integer userId = this.userService.getCurrentUser().getId();
        String date = (String) jp.getArgs()[0];

        List<TimeCardDTO> timeCardDTOList = null;

        try {
            timeCardDTOList = (List<TimeCardDTO>) jp.proceed();
        } catch (Throwable throwable) {
            // 获取异常
            logger.error("[error] list time card, user_id=" + userId + ", date=" + date);
            throw throwable;
        }

        // 获取后
        StringBuilder cardIds = new StringBuilder();
        for (TimeCardDTO timeCardDTO : timeCardDTOList) {
            cardIds.append(timeCardDTO.getId()).append(",");
        }
        logger.info("[success] list time card, user_id=" + userId
                + ", date=" + date
                + ", cardIds=" + cardIds.toString());

        return timeCardDTOList;
    }

    /**
     * 创建时间卡片
     *
     * @param jp 连接点
     * @return 创建时间卡片是否成功
     */
    @Around("execution(* site.beanyon.now.service.TimeCardService.createTimeCard(..))")
    private boolean createTimeCard(ProceedingJoinPoint jp) throws Throwable {
        // 创建前
        TimeCardPO timeCardPO = (TimeCardPO) jp.getArgs()[0];
        Integer userId = this.userService.getCurrentUser().getId();

        // 创建时间卡片是否成功
        boolean result = false;

        try {
            // 创建时间卡片
            result = (Boolean) jp.proceed();
        } catch (Throwable throwable) {
            // 创建异常
            logger.error("[error] create time card, user_id=" + userId);
            throw throwable;
        }

        // 创建后
        logger.info("[success] create time card, user_id=" + userId
                + ", card_id=" + timeCardPO.getId()
                + ", result=" + result);

        return result;
    }

    /**
     * 删除时间卡片
     *
     * @param jp 连接点
     * @return 删除时间卡片是否成功
     */
    @Around("execution(* site.beanyon.now.service.TimeCardService.deleteTimeCard(..))")
    private boolean deleteTimeCard(ProceedingJoinPoint jp) throws Throwable {
        // 删除前
        Integer cardId = (Integer) jp.getArgs()[0];
        Integer userId = this.userService.getCurrentUser().getId();

        boolean result = false;

        try {
            result = (boolean) jp.proceed();
        } catch (Throwable throwable) {
            // 删除异常
            logger.error("[error] delete time card, user_id=" + userId + ", card_id=" + cardId);
            throw throwable;
        }

        // 删除后
        logger.info("[success] delete time card, user_id=" + userId
                + ", card_id=" + cardId
                + ", result=" + result);

        return result;
    }

    /**
     * 更新时间卡片的Activity
     *
     * @param jp 连接点
     * @return 是否更新成功
     */
    @Around("execution(* site.beanyon.now.service.TimeCardService.updateActivity(..))")
    private boolean updateActivity(ProceedingJoinPoint jp) throws Throwable {
        // 更新前
        Integer userId = this.userService.getCurrentUser().getId();
        Integer cardId = (Integer) jp.getArgs()[0];
        String activity = (String) jp.getArgs()[1];

        boolean result = false;

        try {
            result = (boolean) jp.proceed();
        } catch (Throwable throwable) {
            // 更新异常
            logger.error("[error] update time card activity, user_id=" + userId
                    + ", card_id=" + cardId
                    + ", activity=" + activity);
            throw throwable;
        }

        // 更新后
        logger.info("[success] update time card activity, user_id=" + userId
                + ", card_id=" + cardId
                + ", activity=" + activity
                + ", result=" + result);

        return result;
    }
}
